#include "systemsettingwidget.h"
#include "ui_systemsettingwidget.h"
#include "common/touch_calibration.h"
#include "common/touch_calibration_nine.h"
#include <QDesktopWidget>
#include "common/common.h"

SystemSettingWidget::SystemSettingWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::SystemSettingWidget)
{
    ui->setupUi(this);
    setWindowFlags(Qt::FramelessWindowHint);
    setWindowState(Qt::WindowMaximized);
    int ava_memory,total_memory;
    quint64 ava_disk,total_disk;
    platform_sys_info_c::get_memory_info(ava_memory,total_memory);
    platform_sys_info_c::get_disk_info(ava_disk,total_disk);
    ui->ava_memory1->setText(QString::number(ava_memory/1024).append("M"));
    ui->total_memory1->setText(QString::number(total_memory/1024).append("M"));
    ui->ava_disk1->setText(QString::number(ava_disk/(1024*1024)).append("M"));
    ui->total_disk1->setText(QString::number(total_disk/(1024*1024)).append("M"));
    qDebug()<<platform_sys_info_c::get_rtc_time().date();
    ui->dateTimeEdit1->setDateTime(platform_sys_info_c::get_rtc_time());

    ui->dateTimeEdit1->setSelectedSection(QDateTimeEdit::YearSection);
}

SystemSettingWidget::~SystemSettingWidget()
{
    delete ui;
}

void SystemSettingWidget::on_btn_calibrate1_clicked()
{
    if(QApplication::desktop()->height() <= 600)
    {
        touch_calibration dlg2(this);
        dlg2.calibration_exec();

    }
    else
    {
        touch_calibration_nine dlg(this);
        dlg.calibration_exec();
    }
}

void SystemSettingWidget::on_backlightSld1_valueChanged(int value)
{
    qDebug()<<hal_display_get_backlight_brightness();
    qDebug()<<value;
   qDebug()<<"return value:"<<hal_display_set_backlight_brightness(value);
}

//void SystemSettingWidget::on_btn_brightness_clicked()
//{
//    int status =hal_get_lcd_status();
//    if(status==HAL_DISPLAY_BACKLIGHT_CLOSE)
//        hal_set_lcd_status(HAL_DISPLAY_BACKLIGHT_OPEN);
//    else
//        hal_set_lcd_status(HAL_DISPLAY_BACKLIGHT_CLOSE);
//}

void SystemSettingWidget::on_btn_setDatetime1_clicked()
{
//    int year = ui->dateTimeEdit->dateTime().date().year();
//    int month = ui->dateTimeEdit->dateTime().date().month();qDebug()<<month;
//    int day = ui->dateTimeEdit->dateTime().date().day();
//    int hour = ui->dateTimeEdit->dateTime().time().hour();
//    int minute = ui->dateTimeEdit->dateTime().time().minute();
//    int sec = ui->dateTimeEdit->dateTime().time().second();
//    struct tm datetime;
//    datetime.tm_year = year;
//    datetime.tm_mon = month;
//    datetime.tm_mday = day;
//    datetime.tm_hour = hour;
//    datetime.tm_min = minute;
//    datetime.tm_sec = sec;
    QDateTime datetime = ui->dateTimeEdit1->dateTime();
    qDebug()<<platform_sys_info_c::set_rtc_time(datetime);
    QString datetimeStr = datetime.toString("yyyy-MM-dd hh:mm:ss");

    QString dateStr = " date -s \"" + datetimeStr + "\"";
    DoSystem(dateStr.toLatin1().data());

    DoSystem("hwclock -w");



    qDebug()<<QDateTime::currentDateTime();
}

void SystemSettingWidget::on_btn_back1_clicked()
{
    this->deleteLater();
}

void SystemSettingWidget::on_btn_next1_clicked()
{
    QDateTimeEdit * edit = ui->dateTimeEdit1;
    edit->setFocus();
    QDateTimeEdit::Section section = edit->currentSection();
    switch (section) {
    case QDateTimeEdit::YearSection:
        edit->setSelectedSection(QDateTimeEdit::MonthSection);
        break;
    case QDateTimeEdit::MonthSection:
        edit->setSelectedSection(QDateTimeEdit::DaySection);
        break;
    case QDateTimeEdit::DaySection:
        edit->setSelectedSection(QDateTimeEdit::HourSection);
        break;
    case QDateTimeEdit::HourSection:
        edit->setSelectedSection(QDateTimeEdit::MinuteSection);
        break;
    case QDateTimeEdit::MinuteSection:
        edit->setSelectedSection(QDateTimeEdit::SecondSection);
        break;
    case QDateTimeEdit::SecondSection:
        edit->setSelectedSection(QDateTimeEdit::YearSection);
        break;
    default:
        edit->setSelectedSection(QDateTimeEdit::YearSection);
        break;
    }
}


void SystemSettingWidget::on_chk_mousecursor1_clicked()
{
    QWSServer::setCursorVisible(ui->chk_mousecursor1->isChecked());
}
